package com.bnlr.lrparking.appui.mine.record;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.HorizontalAdapter;
import com.bnlr.lrparking.adapter.InvestRecodrAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.model.InvestRecodrData;
import com.bnlr.lrparking.model.InvestRecodrM;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 投资记录
 *
 * @author hp
 * @date 2017/8/23
 */
public class InvestRecordActivity extends BaseActivity {

    private TextView tvNavTitle;
    private HorizontalAdapter horizontalAdapter;
    private ScrollIndicatorView scrollIndicatorView;
    private List<String> list = new ArrayList<>();
    private ListView lv_invest_recodr;
    private LinearLayout ll_invest;
    private InvestRecodrAdapter investRecodrAdapter;
    List<InvestRecodrData> stallList = new ArrayList<>();
    String Status = "-1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_investrecodr);
        initView();
        getListData(Status);
    }

    private void initView() {
        tvNavTitle = (TextView) findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("投资记录");
        ll_invest = (LinearLayout) findViewById(R.id.ll_invest);
        lv_invest_recodr = (ListView) findViewById(R.id.lv_invest_recodr);
        scrollIndicatorView = (ScrollIndicatorView) findViewById(R.id.invest_recodr_scrollIndicatorView);

        list.add("全 部");
        list.add("待付款");
        list.add("已投资");
        list.add("已过期");
        set(scrollIndicatorView, list);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getListData(Status);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            default:
                break;
        }
    }

    /**
     * 获取列表信息
     *
     * @param Status 状态
     */
    public void getListData(String Status) {
        try {
            if (CommonUtil.isNetworkAvailable(baseContext) == 0) {
                CommonUtil.showToask(baseContext, "当前无网络连接，请检查网络设置");
                return;
            }
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "GetApplyList");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("Status", Status);
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<InvestRecodrM>(baseContext, true, InvestRecodrM.class) {
                        @Override
                        public void doWork(InvestRecodrM data, String code) {
                            if ("1".equals(code)) {
                                stallList = data.getData();
                                if (stallList.size() > 0) {
                                    lv_invest_recodr.setVisibility(View.VISIBLE);
                                    ll_invest.setVisibility(View.GONE);
                                    investRecodrAdapter = new InvestRecodrAdapter(baseContext, stallList);
                                    lv_invest_recodr.setAdapter(investRecodrAdapter);
                                } else {
                                    lv_invest_recodr.setVisibility(View.GONE);
                                    ll_invest.setVisibility(View.VISIBLE);
                                }
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("0".equals(code)) {
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    private void set(final Indicator indicator, List list) {
        int selectColor = getResources().getColor(R.color.main_color);
        int unSelectColor = getResources().getColor(R.color.text_color);
        indicator.setAdapter(horizontalAdapter = new HorizontalAdapter(baseContext, list));
        indicator.setScrollBar(new ColorBar(baseContext.getApplicationContext(), selectColor, 4));
        float unSelectSize = 13;
        float selectSize = unSelectSize * 1f;
        indicator.getItemView(1).setMinimumWidth(30);
        indicator.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, unSelectColor).setSize(selectSize, unSelectSize));
        indicator.setItemClickable(true);

        indicator.setOnItemSelectListener(new Indicator.OnItemSelectedListener() {
            @Override
            public void onItemSelected(View selectItemView, int select, int preSelect) {
                if (select == 0) {
                    Status = "-1";
                    getListData(Status);
                } else if (select == 1) {
                    Status = "0";
                    getListData(Status);
                } else if (select == 2) {
                    Status = "1";
                    getListData(Status);
                } else if (select == 3) {
                    Status = "2";
                    getListData(Status);
                }
            }
        });
    }
}
